//
//  MGServiceProvider.m
//  Express
//
//  Created by lhongl on 2025/9/28.
//  Copyright © 2025 QM. All rights reserved.
//

#import "MGServiceProvider.h"
@interface MGServiceProvider ()

/// 协议和类信息
@property (nonatomic, strong) NSMutableDictionary *services;

@end
@implementation MGServiceProvider

+ (instancetype)shareServiceProvider{
    static MGServiceProvider *provider;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        provider = [[self alloc] init];
        provider.services = [NSMutableDictionary dictionary];
    });
    return provider;
}

#pragma mark - 注册
+ (void)registerServiceClass:(Class)cls serviceProvider:(Protocol *)protocol{
    NSAssert(cls && protocol, @"[MG_DEBUG] class and protocol can not nil");
    NSAssert2([cls conformsToProtocol:protocol], @"[MG_DEBUG] class %@ not conform to %@", NSStringFromClass(cls), NSStringFromProtocol(protocol));
    MGServiceProvider *provider = [MGServiceProvider shareServiceProvider];
    [provider.services setObject:cls forKey:NSStringFromProtocol(protocol)];
}

#pragma mark - 获取类
+ (Class)serviceClassForProvider:(Protocol *)protocol{
    NSAssert(protocol, @"[MG_DEBUG]  protocol can not nil");
    MGServiceProvider *provider = [MGServiceProvider shareServiceProvider];
    Class cls = [provider.services objectForKey:NSStringFromProtocol(protocol)];
    return cls;
}

#pragma mark 实例类
+ (id)serviceInstantiationClassForProvider:(Protocol *)protocol{
    Class cls = [self serviceClassForProvider:protocol];
    if (!cls) {
        return nil;
    }
    return [[cls alloc] init];
}


@end
